package mi.payment.ui.director;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import mi.payment.R;
import mi.payment.db.dao.UserInfo;
import mi.payment.ui.base.ParentActivity;
import mi.payment.utils.ThreadPoolManager;
import sunmi.sunmiui.dialog.DialogCreater;
import sunmi.sunmiui.dialog.HintDialog;
import sunmi.sunmiui.title.Title;

/**
 * @author sunmi on 2017/1/8.
 */

public class OperatorAdministratorActivity extends ParentActivity implements OperatorAdminOnClick {

    private OperatorAdminAdapter operatorAdminAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_operator_administr);

//        RelativeLayout relBack = (RelativeLayout) this.findViewById(R.id.rel_back);
//        RelativeLayout relAdd = (RelativeLayout) this.findViewById(R.id.rel_add);
        ListView listview = (ListView) this.findViewById(R.id.list_view);
//        relBack.setOnClickListener(this);
        listview.setVerticalScrollBarEnabled(false);
//        relAdd.setOnClickListener(this);

        Title mTitle = (Title) this.findViewById(R.id.title);
        mTitle.setHeadBgColor(getResources().getColor(R.color.colorBar));
        mTitle.setTitleText(getString(R.string.ordinary_user_administration));
        mTitle.setButtonVisibility(false, false, true, true);
        mTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(OperatorAdministratorActivity.this, UpdatePasswordActivity.class);
                String uid = mUserInfoModel.generateUId();
                UserInfo userInfo2 = new UserInfo();
                userInfo2.setUId(uid);
                intent.putExtra("user_info", userInfo2);
                intent.putExtra("who_update", 4);
                startActivity(intent);
            }
        }, null, null);

        operatorAdminAdapter = new OperatorAdminAdapter(this, this);
        listview.setAdapter(operatorAdminAdapter);

        pullList();
    }

    @Override
    protected void onRestart() {
        pullList();
        super.onRestart();
    }

    private void pullList() {
        ThreadPoolManager.executeInCachePool(new Runnable() {
            @Override
            public void run() {
                final List<UserInfo> result = new ArrayList<>();
                List<UserInfo> list = mUserInfoModel.queryAllUser();
                for (UserInfo userInfo : list) {
                    if (userInfo.getUType() == 1) {
                        result.add(userInfo);
                    }
                }
                runOnUI(new Runnable() {
                    @Override
                    public void run() {
                        operatorAdminAdapter.setData(result);
                    }
                });
            }
        });
    }

    @Override
    public void editOnClick(UserInfo userInfo) {
        Intent intent = new Intent(this, UpdatePasswordActivity.class);
        intent.putExtra("user_info", userInfo);
        intent.putExtra("who_update", 2);
        startActivity(intent);
    }

    private HintDialog hintDialog;

    @Override
    public void deleteOnClick(final UserInfo userInfo) {
        hintDialog = DialogCreater.createHintDialog(this, sunmi.sunmiui.R.drawable.img_warning,
                getString(R.string.operatoradmin_cancel),
                getString(R.string.operatoradmin_confirm),
                getString(R.string.operatoradmin_info) + userInfo.getUId() + "”",

                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        hintDialog.dismiss();
                    }

                }, new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        mUserInfoModel.deleteUserById(userInfo.getUId());
                        pullList();
                        hintDialog.dismiss();
                    }

                }, false);

        hintDialog.show();
    }

}
